﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace SimpComm
{
    static public partial class ElementExtensions
    {
        public static MvcHtmlString Niko_DropDown<TModel, TProperty, BindType>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, List<MyEnum<BindType>> DataBinds, BindType defaultValue = default(BindType)) where BindType : struct
        {
            if (expression == null)
                throw new ArgumentNullException("expression");

            StringBuilder sb = new StringBuilder();
            sb.Append("<div class=\"control-group\">");
            sb.Append("<label class=\"control-label\">");
            var LabelHtmlString = LabelExtensions.LabelFor<TModel, TProperty>(htmlHelper, expression);
            sb.Append(LabelHtmlString);
            sb.Append("</label>");
            sb.Append("<div class=\"controls\">");
            var HiddenHtmlString = InputExtensions.HiddenFor<TModel, TProperty>(htmlHelper, expression, (IDictionary<string, object>)null);
            sb.Append(HiddenHtmlString);
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
            var name = metadata.PropertyName;
            sb.Append("<select class=\"input-xlarge\" id=\"" + name + "_Select\">");
            foreach (var item in DataBinds)
            {
                if (defaultValue.Equals(item.Value))
                {
                    sb.Append("<option value=\"" + item.Value + "\">" + item.Text + "</option>");

                }
                else
                {

                    sb.Append("<option value=\"" + item.Value + "\">" + item.Text + "</option>");
                }
            }
            sb.Append("</select>");
            var validat = ValidationExtensions.ValidationMessageFor<TModel, TProperty>(htmlHelper, expression, (string)null,
                                                                         (IDictionary<string, object>)
                                                                         new RouteValueDictionary());
            sb.Append(validat);
            sb.Append("</div>");
            sb.Append("</div>");
            sb.Append("<script type=\"text/javascript\">");
            sb.Append("$(\"#" + name + "_Select\").change(function (e) {");
            sb.Append("window.IsValid = true;");
            sb.Append("$(\"#Data_" + name + "\").val($(\"#" + name + "_Select\").val());");
            sb.Append("});");
            
//                        $(function () {
//                            var defaultvalue = $("#Data_AdmColID").val();
//                            $("#AdmColID_Select").val(defaultvalue);
//                        });
            sb.Append("$(function () {");
            sb.Append("var defaultvalue = $(\"#Data_" + name + "\").val();");
            sb.Append("$(\"#" + name + "_Select\").val(defaultvalue);");
            sb.Append("});");

            sb.Append("</script>");
            return new MvcHtmlString(sb.ToString());
        }
    }
}
